#include "HiKernel/HiFramework.h"
#include "HiKernel/HiGBRenderer.h"
#include "HiModules/mHiDummy/HiDummy.h"
#include "HiModules/mHiDummy/LM_Anim.h"



mHiDummy::mHiDummy(HiXmlNode*xml)
{
//	NiRenderer* pkRenderer = NiRenderer::GetRenderer();

	NiInit();
}


void
mHiDummy::PostConfig()
{
	m_gbRender  = (HiGBRenderer*) HiViewer::GetInstance()->GetRenderer();

	if ( m_gbRender ->GetRenderType() != HiRenderer::Gamebryo )
		exit(-1);

	
	m_gbRender ->Load("WORLD.NIF");
	m_gbRender ->LoadCharacter("KFM/LM.kfm");

	
	const unsigned int uiScreenWidth = m_gbRender ->m_spRenderer->
        GetDefaultRenderTargetGroup()->GetWidth(0);
    const unsigned int uiScreenHeight = m_gbRender ->m_spRenderer->
        GetDefaultRenderTargetGroup()->GetHeight(0);

#if defined(_PS3) || defined(_XENON)    
    const float fDesiredAspect = 1280.0/720.0;
#else
    const float fDesiredAspect = 4.0f/3.0f;
#endif

	float fAspect = (float)uiScreenWidth/(float)uiScreenHeight;
    if (NiAbs(fAspect - fDesiredAspect) > 0.01f)
    {
        // bad aspect ratio
        //WorldManager::Log("MetalWars: Splash aspect incorrect.\n");
    }

  /*  m_pkSplash = NiNew NiScreenElements(
        NiNew NiScreenElementsData(false, false, 1));*/

	m_pkSplash = m_gbRender->NewScreenElements();

    // We know that the polygon handle is zero and will use it directly in
    // the vertex and texture coordinate assignments.
    m_pkSplash->Insert(4);
    m_pkSplash->SetRectangle(0, 0.0f, 0.0f, 1.0f, 1.0f);
    m_pkSplash->UpdateBound();

    // We assume the splash screen is already the correct aspect ratio, and
    // can thus render the entire texture
    m_pkSplash->SetTextures(0, 0, 0.0f, 0.0f, 1.0f, 1.0f);

    // use a texture
    //NiTexturingProperty* pkTex = NiNew NiTexturingProperty(
    //    "Splash.dds");

	NiTexturingProperty* pkTex = m_gbRender->NewTexturingProperty();

    pkTex->SetBaseClampMode(NiTexturingProperty::CLAMP_S_CLAMP_T);
    pkTex->SetApplyMode(NiTexturingProperty::APPLY_REPLACE);
    m_pkSplash->AttachProperty(pkTex);

    // no alpha blending
    NiAlphaProperty* pkAlpha = NiNew NiAlphaProperty;
    pkAlpha->SetAlphaBlending(false);
    m_pkSplash->AttachProperty(pkAlpha);

    m_pkSplash->UpdateProperties();
    m_pkSplash->Update(0.0f);



	
}

void mHiDummy::Update()
{
	//if ( m_gbRender->GetRenderType() != HiRenderer::Gamebryo )
	//	exit(-1);

	if(GetAsyncKeyState('E'))
		m_gbRender->SetTargetAnimation(LM_Anim::JUMP01);
}

void mHiDummy::Render()
{
	m_gbRender->m_spRenderer->SetScreenSpaceCameraData();
    m_pkSplash->Draw(m_gbRender->m_spRenderer);
}

extern "C" HI_DLLEXPORT
mHiDummy* HiLoadModule_mHiDummy(HiXmlNode* xml)
{
	mHiDummy *m= new mHiDummy(xml);
	return m;
}
